// pages/web_view/web_view.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    url: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.target_type) {
      var type = options.target_type;
      var resourceId = options.target_id;
      var taskId = options.task_id;
      var kmappingId = options.kmapping_id;
      var structureId = options.structure_id;
      var that = this;
      that.setData({
        resource_id: resourceId,
        type: type,
        task_id: taskId,
        structure_id: structureId,
        kmapping_id: kmappingId
      })
      that.getRsourceDetailInfo();
      that.getaddTaskLogFirst(2);
    } else {
      var link = options.url + "?";
      for (var key in options) {
        if (key != 'url') {
          link += key + '=' + options[key] + '&';
        }
      }
      link += 'token=' + wx.getStorageSync('token');
      this.setData({
        url: link
      })
    }

  },
  getRsourceDetailInfo() {
    var that = this;
    wx.request({
      url: app.globalData.URL + '/kmapping/WebAppAjax/getRsourceDetailInfo?' + app.globalData.URLBOOL,
      dataType: 'json',
      method: 'GET',
      data: {
        resource_id: that.data.resource_id,
        type: that.data.type,
        task_id: that.data.task_id,
        structure_id: that.data.structure_id,
        kmapping_id: that.data.kmapping_id
      },
      success: function(res) {
        var _res = res.data;
        if (_res.error_code == 0) {
          if (that.data.type == 404) {
            var url = _res.data.resource_info.resource_info.weixinapp_url + '&' + app.globalData.URLBOOL;
          } else if (that.data.type == 214) {
            var url = _res.data.resource_info.imgtext_info.url + '&' + app.globalData.URLBOOL;
          }
          that.setData({
            "url": url
          })
        }
      }
    })
  },
  getaddTaskLogFirst(type) {
    var that = this;
    wx.request({
      url: app.globalData.URL + '/kmapping/WebAppAjax/addTaskLog?' + app.globalData.URLBOOL,
      dataType: 'json',
      method: 'GET',
      data: {
        resource_id: that.data.resource_id,
        type: type,
        task_id: that.data.task_id,
        structure_id: that.data.structure_id,
        kmapping_id: that.data.kmapping_id
      },
      success: function() {
        that.getaddTaskLogSecond(3)
      }
    })
  },
  getaddTaskLogSecond(type) {
    var that = this;
    wx.request({
      url: app.globalData.URL + '/kmapping/WebAppAjax/addTaskLog?' + app.globalData.URLBOOL,
      dataType: 'json',
      method: 'GET',
      data: {
        resource_id: that.data.resource_id,
        type: type,
        task_id: that.data.task_id,
        structure_id: that.data.structure_id,
        kmapping_id: that.data.kmapping_id
      },
      success: function(res) {}
    })
  },
  /**
   * 监听H5向小程序传递的数据
   */
  getMessage(e) {
    console.log('getMessage event:',e);
    var _this = this; return;
    var index = e.detail.data.length - 1;
    _this.setData({
      problemArray: e.detail.data[index]
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    var _this = this,
      _problemArray = _this.data.problemArray;
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面
    prevPage.setData({
      //problemArray: _problemArray,
      onlineState: true
    });
    return;
    if (_problemArray) {
      var problemArray = JSON.parse(_problemArray);
      var _problemArray = prevPage.data.problemArray;
      for (var i = 0; i < _problemArray.length; i ++){
        if (_problemArray[i].id == problemArray.id){
          _problemArray[i] = problemArray
        }
      }
      
      // console.log([problemArray]);
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})